package org.ipmsg.core.file;

import org.ipmsg.core.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.util.*;

public class PublishFiles extends Thread {

    private ServerSocket serverSocket;
    private Hashtable filesHashTable;
    private int maxBuffer;

    public PublishFiles(ServerSocket serverSocket, int maxBuffer) {
        this.serverSocket = serverSocket;
        this.maxBuffer = maxBuffer;
        filesHashTable = new Hashtable();
        start();
    }

    @Override
    public void run() {
        byte tmpbuf[] = new byte[maxBuffer];
        while (true) {
            try {
                Socket clientSocket;
                int read;
                clientSocket = serverSocket.accept();
                InputStream requestInputStream = clientSocket.getInputStream();
                read = requestInputStream.read(tmpbuf);
                if (read <= 0) {
                    clientSocket.close();
                    continue; /* Loop/switch isn't completed */
                }
                byte buffer[];
                buffer = new byte[read];
                System.arraycopy(tmpbuf, 0, buffer, 0, buffer.length);
                if ((long) Character.getNumericValue((char) buffer[0]) != 1L) {
                    clientSocket.close();
                    continue; /* Loop/switch isn't completed */
                }
                Message ipmsgPack = new Message(buffer);
                StringTokenizer stringTokenizer = new StringTokenizer(ipmsgPack.getContent(), ":", false);
                String packageID = stringTokenizer.hasMoreTokens() ? stringTokenizer.nextToken() : "";
                String fileID = stringTokenizer.hasMoreTokens() ? stringTokenizer.nextToken() : "";
                String inetAddress = clientSocket.getInetAddress().getHostAddress();
                String requestHeader = (new StringBuilder(String.valueOf(inetAddress))).append(":").append(packageID).append(":").append(fileID).toString();
                if (!filesHashTable.containsKey(requestHeader)) {
                    clientSocket.close();
                    continue; /* Loop/switch isn't completed */
                }

                String filePath = (String) filesHashTable.get(requestHeader);
                long command = ipmsgPack.getCommand() & 255L;
                new TransferFile(clientSocket, filePath, command);
                removeAttachment(requestHeader);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    public void addAttachment(Address inetAddress, Message pack) {
        Attachment attachment = pack.getAttachment();
        if (attachment == null) {
            return;
        }
        if (!attachment.hasFiles()) {
            return;
        }
        for (int i = 0; i < attachment.countFiles(); i++) {
            String key = (new StringBuilder(String.valueOf(inetAddress.getInetAddress().getHostAddress()))).append(":").toString();
            key = (new StringBuilder(String.valueOf(key))).append(Long.toHexString(pack.getSerial())).append(":").toString();
            key = (new StringBuilder(String.valueOf(key))).append(Long.toHexString(attachment.getFileID(i))).toString();
            filesHashTable.put(key, attachment.getFilePath(i));
        }

    }

    public void addAttachment(Address inetAddress[], Message pack) {
        if (pack.getAttachment() == null) {
            return;
        }
        for (int i = 0; i < inetAddress.length; i++) {
            addAttachment(inetAddress[i], pack);
        }

    }

    public void removeAttachment(String key) {
        filesHashTable.remove(key);
    }

    public void removeAttachment(Address inetAddress, Message pack) {
        String str = (new StringBuilder(String.valueOf(inetAddress.getInetAddress().getHostAddress()))).append(":").toString();
        str = (new StringBuilder(String.valueOf(str))).append(Long.toHexString(Long.parseLong(pack.getContent()))).toString();
        for (Enumeration em = filesHashTable.keys(); em.hasMoreElements();) {
            String key = (String) em.nextElement();
            if (key.startsWith(str)) {
                removeAttachment(key);
            }
        }

    }
}
